package com.dfs.parent.order.service.impl;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfs.parent.common.core.domain.Result;
import com.dfs.parent.order.dto.OrderDTO;
import com.dfs.parent.order.entity.Order;
import com.dfs.parent.order.mapper.OrderMapper;
import com.dfs.parent.order.service.OrderService;
import com.dfs.parent.service.dto.ProductDTO;
import com.dfs.parent.service.feign.ProductFeignClient;
import com.dfs.parent.service.vo.ProductVO;
import lombok.RequiredArgsConstructor;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    private final ProductFeignClient productFeignClient;
    @Autowired
    private OrderMapper orderMapper;


    @Trace
    @Override
    public ProductVO createOrder(OrderDTO order, HttpServletRequest request) throws Exception {
        ProductDTO productDTO = new ProductDTO();
        productDTO.setId(order.getProductId());
        productDTO.setCount(100);
        System.out.println("==========111======");
//        Result<ProductVO> result =  productFeignClient.reduceCount(productDTO);

        Order order1 = new Order();
        order1.setCount(order.getCount());
        order1.setUserId(order.getUserId());
       if( orderMapper.insert(order1)>0){
           System.out.println("11111======");
       }
//        if(result.getCode()!=200){
//            throw new Exception(result.getMsg());
//        }
//        System.out.println(result.getData());
//        return result.getData();

        ProductVO productVO = new ProductVO();
        productVO.setId(order.getProductId());
        productVO.setCount(100);
        return productVO;

    }

    @Override
    public Order findById(Long id) {
//        Order order1 = new Order();
//        order1.setCount(100);
//        order1.setUserId(100011L);
//        if( baseMapper.insert(order1)>0){
//            System.out.println("11111======");
//        }
        // 强制从主库查询数据
     //   HintManager.getInstance().setMasterRouteOnly();
        return orderMapper.selectById(id);
    }

    @Override
    public Result<String> orderConfig() {
        return productFeignClient.config();
    }
}
